package com.my.radio;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.hardware.mrd.AkRadio;
import com.my.radio.R;
public class FrequencyListView extends ListView {

	public static int DEFAULTFGCOLOR;
	public static int SELECTEDCOLOR;
	private List<FrequencyView> viewList;
	public FrequencyListViewAdapter mFreqAdapter;
	private RadioActivity mRadioActivity;
	
	public FrequencyListView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public FrequencyListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.RadioFrequencyList);
		// setFocusable(true);
		// setFocusableInTouchMode(true);

		DEFAULTFGCOLOR = a.getColor(
				R.styleable.RadioFrequencyList_defaultFGColor,
				android.R.color.black);
		SELECTEDCOLOR = a.getColor(
				R.styleable.RadioFrequencyList_selectedTextColor,
				android.R.color.black);
		
		viewList = new ArrayList<FrequencyView>(AkRadio.mFreqListSize);
		getViews();
		
		mFreqAdapter = new FrequencyListViewAdapter(getContext(), viewList);
		setAdapter(mFreqAdapter);


		setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				FrequencyView fv = viewList.get(position);//(FrequencyView) view;				
				FrequencyView.mSelected_ID = (byte) (position + 1);
				mFreqAdapter.setSelectItem(position);
				postInvalidate();
				if (mRadioActivity != null) {
					mRadioActivity.radio_frequency_button_onClick((byte) (fv
							.getmID() - 1), true);
				}
				
			}
		});

		this.setOnItemLongClickListener(new OnItemLongClickListener() {

			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				FrequencyView fv = viewList.get(position);//(FrequencyView) view;	
				if (mRadioActivity != null) {
					mRadioActivity.radio_frequency_button_onClick((byte) (fv
							.getmID() - 1), false);
				}
				return true;
			}

		});
	}

	public FrequencyListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub

	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		return super.onTouchEvent(event);
	}

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
	}

	private void getViews() {
		for (int i = 0; i < AkRadio.mFreqListSize; i++) {
			viewList.add(new FrequencyView(getContext(), i+1));
		}
	}

	public List<FrequencyView> getViewList() {
		return viewList;
	}

	public void setViewList(List<FrequencyView> viewList) {
		this.viewList = viewList;
	}

	public void setRaidoActivity(RadioActivity ac){
		mRadioActivity = ac;
	}
}
